$(function(){
	var basePath=$("#basepath").val();
	//筛选按钮
	$("#classFilter_go").click(function(){
		var clgId=$("#classFilter_form select[name='college']").val();
		window.location=basePath+"admin/class/showClassList/"+clgId+"/1";
	});
	//编辑按钮
	$(".class_edit").click(function(){
		var classId=$(this).attr("data-classId");
		$("#class_edit_box_alert_container").html("");//清空提示框
		$.get(basePath+ "admin/class/getClass/"+classId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						var clgId=data.branch.prof.department.college.id;
						var depId=data.branch.prof.department.id;
						var proId=data.branch.prof.id;
						var branchId=data.branch.id;
						$("#class_edit_box_form input[name='class_no']").val(data.no);
						$("#class_edit_box_form input[name='class_name']").val(data.name);
						$("#class_edit_box_form input[name='class_id']").val(data.id);
						$("#class_edit_box_form select[name='college']").val(clgId);
						$("#class_edit_box_form select[name='department']").attr("data-org",depId);
						$("#class_edit_box_form select[name='profession']").attr("data-org",proId);
						$("#class_edit_box_form select[name='branch']").attr("data-org",branchId);
						$("#class_edit_box_form select[name='college']").change();
					}
		},"json");
	});
	$("#class_edit_box_form select[name='college']").change(function(e){
		var collegeId=$(this).val();
		var $depSelect=$("#class_edit_box_form select[name='department']");
		$depSelect.html("");
		var depId=$depSelect.attr("data-org");
		$depSelect.attr("data-org","0");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							 $(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(depId>0){
							$depSelect.val(depId);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($depSelect);
							makeAlert("无可用系,请添加之后再试","error",$("#class_edit_box_alert_container"),"no_dep_error");
						}else{
							$("#class_edit_box_alert_container .no_dep_error").alert('close');
						}
						$depSelect.change();
					}
			}, "json");
	});
	$("#class_edit_box_form select[name='department']").change(function(){
		var departmentId=$(this).val();
		var $proSelect=$("#class_edit_box_form select[name='profession']");
		$proSelect.html("");
		var proId=$proSelect.attr("data-org");
		$proSelect.attr("data-org","0");
		$.get(basePath+ "admin/branch/getProsOfDeps/"+departmentId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($proSelect);
						}
						if(proId>0){
							$proSelect.val(proId);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($proSelect);
							makeAlert("无可用专业,请添加之后再试","error",$("#class_edit_box_alert_container"),"no_pro_error");
						}else{
							$("#class_edit_box_alert_container .no_pro_error").alert('close');
						}
						$proSelect.change();
					}
			}, "json");
	});
	$("#class_edit_box_form select[name='profession']").change(function(){
		var proId=$(this).val();
		var $branchSelect=$("#class_edit_box_form select[name='branch']");
		$branchSelect.html("");
		var branchId=$branchSelect.attr("data-org");
		$branchSelect.attr("data-org","0");
		$.get(basePath+ "admin/branch/getBranchesOfPro/"+proId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($branchSelect);
						}
						if(branchId>0){
							$branchSelect.val(branchId);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($branchSelect);
							makeAlert("无可用专业方向,请添加之后再试","error",$("#class_edit_box_alert_container"),"no_branch_error");
						}else{
							$("#class_edit_box_alert_container .no_branch_error").alert('close');
						}
						$("#class_edit_box .ajax_wait").hide();
						$("#class_edit_box_form").show();
					}
			}, "json");
	});
	//修改保存
	$("#class_edit_box_save").click(function(){
		var departmentId=$("#class_edit_box_form select[name='department']").val();
		var professionId =$("#class_edit_box_form select[name='profession']").val();
		var branchId =$("#class_edit_box_form select[name='branch']").val();
		var classId =$("#class_edit_box_form input[name='class_id']").val();
		var classNo =$("#class_edit_box_form input[name='class_no']").val();
		var className =$("#class_edit_box_form input[name='class_name']").val();
		if(departmentId==0){
			makeAlert("无可用系,请添加之后再试","error",$("#class_edit_box_alert_container"),"no_dep_error");
			return;
		}
		if(professionId==0){
			makeAlert("无可用专业,请添加之后再试","error",$("#class_edit_box_alert_container"),"no_pro_error");
			return;
		}
		if(branchId==0){
			makeAlert("无可用专业方向,请添加之后再试","error",$("#class_edit_box_alert_container"),"no_branch_error");
			return;
		}
		$("#class_edit_box_alert_container .empty_field_error").alert('close');
		$("#class_edit_box_alert_container .repeat_no_error").alert('close');
		$("#class_edit_box_alert_container .repeat_name_error").alert('close');
		$.post(basePath+ "admin/class/updateClass",{id:classId,no:classNo,name:className,prof_id:branchId},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-3":
							makeAlert("字段不允许为空,请重新检查","error",$("#class_edit_box_alert_container"),"empty_field_error");
							break;
						case "-4":
							makeAlert("已存在编号为"+classNo+"的班级,请检查后再输入","error",$("#class_edit_box_alert_container"),"repeat_no_error");
							break;
						case "-5":
							makeAlert("已存在班级 "+className+",请检查后再输入","error",$("#class_edit_box_alert_container"),"repeat_name_error");
							break;
						case "-6":
							alert("参数错误,请重试");
							history.go(0);
							break;
						case "0":
							noty({text:"更新成功,正在刷新"});
							$("#class_edit_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	$(".class_delete").click(function(){
		$("#class_delete_box input[name='classId']").val($(this).attr("data-classId"));
	});
	$("#class_delete_box_delete").click(function(){
		var classId=$("#class_delete_box input[name='classId']").val();
		$.get(basePath+ "admin/class/deleteClass/"+classId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							noty({text:"删除错误:此班级之下还有未删除的学生",type:"error"});
							$("#class_delete_box").modal('hide');
							break;
						case "0":
							noty({text:"删除成功,正在刷新"});
							$("#class_delete_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	$("#class_add").click(function(){
		$("class_add_box_alert_container").html("");
		$("#class_add_box_form select[name='college']").change();
	});
	$("#class_add_box_form select[name='college']").change(function(e){
		var collegeId=$(this).val();
		var $depSelect=$("#class_add_box_form select[name='department']");
		$depSelect.html("");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							 $(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($depSelect);
							makeAlert("无可用系,请添加之后再试","error",$("#class_add_box_alert_container"),"no_dep_error");
						}else{
							$("#class_add_box_alert_container .no_dep_error").alert('close');
						}
						$depSelect.change();
					}
			}, "json");
	});
	$("#class_add_box_form select[name='department']").change(function(){
		var departmentId=$(this).val();
		var $proSelect=$("#class_add_box_form select[name='profession']");
		$proSelect.html("");
		$.get(basePath+ "admin/branch/getProsOfDeps/"+departmentId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($proSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($proSelect);
							makeAlert("无可用专业,请添加之后再试","error",$("#class_add_box_alert_container"),"no_pro_error");
						}else{
							$("#class_add_box_alert_container .no_pro_error").alert('close');
						}
						$proSelect.change();
					}
			}, "json");
	});
	$("#class_add_box_form select[name='profession']").change(function(){
		var proId=$(this).val();
		var $branchSelect=$("#class_add_box_form select[name='branch']");
		$branchSelect.html("");
		$.get(basePath+ "admin/branch/getBranchesOfPro/"+proId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($branchSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($branchSelect);
							makeAlert("无可用专业方向,请添加之后再试","error",$("#class_add_box_alert_container"),"no_branch_error");
						}else{
							$("#class_add_box_alert_container .no_branch_error").alert('close');
						}
					}
			}, "json");
	});
	$("#class_add_box_save").click(function(){
		var departmentId=$("#class_add_box_form select[name='department']").val();
		var professionId =$("#class_add_box_form select[name='profession']").val();
		var branchId =$("#class_add_box_form select[name='branch']").val();
		var classNo =$("#class_add_box_form input[name='class_no']").val();
		var className =$("#class_add_box_form input[name='class_name']").val();
		if(departmentId==0){
			makeAlert("无可用系,请添加之后再试","error",$("#class_add_box_alert_container"),"no_dep_error");
			return;
		}
		if(professionId==0){
			makeAlert("无可用专业,请添加之后再试","error",$("#class_add_box_alert_container"),"no_pro_error");
			return;
		}
		if(branchId==0){
			makeAlert("无可用系,请添加之后再试","error",$("#class_add_box_alert_container"),"no_branch_error");
			return;
		}
		$("#class_add_box_alert_container .empty_field_error").alert('close');
		$("#class_add_box_alert_container .repeat_no_error").alert('close');
		$("#class_add_box_alert_container .repeat_name_error").alert('close');
		$.post(basePath+ "admin/class/insertClass/",{no:classNo,name:className,prof_id:branchId},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							makeAlert("字段不允许为空,请重新检查","error",$("#class_add_box_alert_container"),"empty_field_error");
							break;
						case "-3":
							makeAlert("已存在编号为"+classNo+"的班级,请检查后再输入","error",$("#class_add_box_alert_container"),"repeat_no_error");
							break;
						case "-4":
							makeAlert("已存在班级 "+className+",请检查后再输入","error",$("#class_add_box_alert_container"),"repeat_name_error");
							break;
						case "-5":
							alert("参数错误,请重试");
							history.go(0);
							break;
						case "0":
							noty({text:"添加成功,正在刷新"});
							$("#class_add_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	//批量创建
	$("#class_add_bulk").click(function(){
		$("class_add_box_bulk_alert_container").html("");
		$("#class_add_box_bulk_form select[name='college']").change();
		var $countSelect=$("#class_add_box_bulk_form select[name='count']").html("");
		for(i=1;i<=10;i++){
			 $(" <option/>").val(i).html(i).appendTo($countSelect);
		}
	});
	$("#class_add_box_bulk_form select[name='college']").change(function(e){
		var collegeId=$(this).val();
		var $depSelect=$("#class_add_box_bulk_form select[name='department']");
		$depSelect.html("");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							 $(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($depSelect);
							makeAlert("无可用系,请添加之后再试","error",$("#class_add_box_bulk_alert_container"),"no_dep_error");
						}else{
							$("#class_add_box_bulk_alert_container .no_dep_error").alert('close');
						}
						$depSelect.change();
					}
			}, "json");
	});
	$("#class_add_box_bulk_form select[name='department']").change(function(){
		var departmentId=$(this).val();
		var $proSelect=$("#class_add_box_bulk_form select[name='profession']");
		$proSelect.html("");
		$.get(basePath+ "admin/branch/getProsOfDeps/"+departmentId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($proSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($proSelect);
							makeAlert("无可用专业,请添加之后再试","error",$("#class_add_box_bulk_alert_container"),"no_pro_error");
						}else{
							$("#class_add_box_bulk_alert_container .no_pro_error").alert('close');
						}
						$proSelect.change();
					}
			}, "json");
	});
	$("#class_add_box_bulk_form select[name='profession']").change(function(){
		var proId=$(this).val();
		var $branchSelect=$("#class_add_box_bulk_form select[name='branch']");
		$branchSelect.html("");
		$.get(basePath+ "admin/branch/getBranchesOfPro/"+proId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($branchSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($branchSelect);
							makeAlert("无可用专业方向,请添加之后再试","error",$("#class_add_box_bulk_alert_container"),"no_branch_error");
						}else{
							$("#class_add_box_bulk_alert_container .no_branch_error").alert('close');
						}
					}
			}, "json");
	});
	$("#class_add_bulk_box_save").click(function(){
		var departmentId=$("#class_add_box_bulk_form select[name='department']").val();
		var professionId =$("#class_add_box_bulk_form select[name='profession']").val();
		var branchId =$("#class_add_box_bulk_form select[name='branch']").val();
		var classNo =$("#class_add_box_bulk_form input[name='class_no']").val();
		var className =$("#class_add_box_bulk_form input[name='class_name']").val();
		var count =$("#class_add_box_bulk_form select[name='count']").val();
		if(departmentId==0){
			makeAlert("无可用系,请添加之后再试","error",$("#class_add_box_bulk_alert_container"),"no_dep_error");
			return;
		}
		if(professionId==0){
			makeAlert("无可用专业,请添加之后再试","error",$("#class_add_box_bulk_alert_container"),"no_pro_error");
			return;
		}
		if(branchId==0){
			makeAlert("无可用系,请添加之后再试","error",$("#class_add_box_bulk_alert_container"),"no_branch_error");
			return;
		}
		if(count<=0){
			alert("错误的班级数目,请重试");
			history.go(0);
			return;
		}
		$("#class_add_box_bulk_alert_container .empty_field_error").alert('close');
		$.post(basePath+ "admin/class/insertClassBulk/",{no:classNo,name:className,prof_id:branchId,count:count},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							makeAlert("字段不允许为空,请重新检查","error",$("#class_add_box_bulk_alert_container"),"empty_field_error");
							break;
						case "0":
							noty({text:"添加成功,正在刷新"});
							$("#class_add_box_bulk").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
});